package com.gym.myshop.web.controller;

import com.gym.myshop.common.Const;
import com.gym.myshop.entity.User;
import com.gym.myshop.services.UserServices;
import com.gym.myshop.services.impl.UserServicesImpl;
import com.gym.myshop.util.Base64Utils;
import com.gym.myshop.util.MD5Utils;
import com.gym.myshop.util.RandomUtils;
import com.gym.myshop.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ming
 * 2021/11/3
 * description:
 */
@WebServlet("/user")
public class UserController extends BaseServlet {

    private UserServices us = new UserServicesImpl();
    private String activate;


    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:清除登录信息
     */
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //销毁session
        request.getSession().invalidate();
        //清除cookie中自动登录信息
        Cookie c = new Cookie(Const.UserConst.AUTO_LOGIN,"");
        c.setMaxAge(0);
        c.setPath("/");
        response.addCookie(c);
        //登录
        return Const.REDIRECT+"index.jsp";
    }


    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:登录
     */
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");
        //访问业务层
        User ans = us.login(username, password);
        if (ans != null) {
            //保存数据到session
            request.getSession().setAttribute(Const.UserConst.IS_LOGIN, ans);
            if (StrUtils.isNotEmpty(auto)) {
                //用户选择保存2周自动登录
                String userInfo = username+":"+password;
                Cookie c = new Cookie(Const.UserConst.AUTO_LOGIN, Base64Utils.encode(userInfo));
                //时间设置
                c.setMaxAge(60*60*24*14);
                //作用范围
                c.setPath("/");
                //response，保存
                response.addCookie(c);
            }
            return Const.REDIRECT + "index.jsp";
        } else {
            request.setAttribute("msg", "登录失败");
            return Const.FORWARD + "login.jsp";
        }
    }


    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:邮件激活
     */
    public String activate(HttpServletRequest request, HttpServletResponse response) {
        String c = request.getParameter("c");
        String e = request.getParameter("e");
        //访问业务层
        String msg = activate;
        //不同页面间传值,一次使用，再传就要再设一个`
        request.setAttribute("msg", msg);
        return Const.FORWARD + "message.jsp";
    }


        /**
         *@author:ming
         *@Date: 2021/11/4
         *@description:注册
         */
    public String register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        User u = new User(username, MD5Utils.md5(password), email, gender,
                0, RandomUtils.createActive(), 0);
        //业务层
        boolean ans = us.addUser(u);
//        System.out.println("这是ans"+ans);
        if (ans) {
            return Const.REDIRECT + "index.jsp";
        } else {
            //不同页面间传值,一次使用，再传就要再设一个
            request.setAttribute("msg", "注册失败,请重试");
            return Const.FORWARD + "register.jsp";
        }

    }

    /**
     *@author:ming
     *@Date: 2021/11/4
     *@description:用于检测用户名是否存在的请求
     */
    public String checkname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取username验证请求
        String username = request.getParameter("username");
        //业务层
        User user = us.checkName(username);
        if (user !=null) {
            //查询到有存在返回1
            return Const.FAIL;
        }
        //没有查询到返回0
        return Const.SUCCESS;
    }

}
